<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of channelmanager
 *
 * @author dragon.hu add @2016.5.3
 */
defined('BASEPATH') or die('forbidden');

class Channelmgr_Controller extends Controller {
    private $tmpconnect;
    
    public function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        $config = get_config();

        $this->tmpconnect = $this->load->database('center_db') or die("load failed!".mysql_error());
    }

    function index() {
        $this->view->display("channel/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $rows = array();
        $sql = "SELECT COUNT(*) AS num FROM tt_channel WHERE {$where}";
        $one = $this->tmpconnect->one($sql);
        $total = $one['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder}" : "";
        $this->tmpconnect->query("SELECT {$colsinfo} FROM tt_channel WHERE {$where} {$orderby} LIMIT {$offset},{$rp}");
        while ($n = $this->tmpconnect->get_array('default')) {
            $rows[] = array('id' => $n['id'], 'cell' => $n);
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

function add()
	{
		if (is_submit())
		{
			$rowdata = $_POST['row'];
			$id = $rowdata['channelid'];
			$channel = $rowdata['channel'];
			$manager = $this->auth->fields['username'];
			$addtime = date('Y-m-d H:i:s', time());
			$sql = "INSERT INTO tt_channel (id, channel, manager, status, addtime) VALUES ('$id', '$channel', '".$manager."','0', '".$addtime."')";

			if($this->tmpconnect->execute($sql))
			{
				insert_log(translate('MENU_GENERAL_CHANNEL'),translate('EDIT'), NULL, $rowdata);
				cp_tips("success");
			}
		}

		$this->view->display("channel/add");
	}

	function del($id = 0)
	{ 
		if ($this->tmpconnect->delete('tt_channel', array('id' => $id))) {
			insert_log(translate('MENU_GENERAL_CHANNEL'),translate('DEL'), array('id'=> $id));
			$c = 1;
		} else {
			$r = translate('C_TIPS_ERROR_TEXT');
		}

		json_output($c, $r);
	}
    
}
